package com.blork.rag;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class RagEvent {
	
	private JSONObject event;
	
	public Boolean require_tshirt_size;
	public long location_long;
	public Boolean open;
	public Date date_end;
	public String tagline;
	public Date date_start;
	public long location_lat;
	public String location_name;
	public String ticket_price;
	public String money_raised;
	public String title;
	public Boolean require_address;
	public int id;
	public int event_id;
	public Date sponsorship_deadline;
	public String sponsorship_min;
	public Boolean require_passport;
	public int past_event_id;
	public String logo_id;
	public Date signup_end;
	public Boolean require_emergency;
	public int attendees;
	public Boolean require_doctor;
	public int capacity;
	public Date signup_start;
	public String description;
	public String fb_eid;
	public Boolean require_hoody_size;
	
	public RagEvent(Cursor cursor) throws ParseException{

		tagline = cursor.getString(cursor.getColumnIndex("tagline"));
		location_lat = cursor.getLong(cursor.getColumnIndex("location_lat"));
		location_name = cursor.getString(cursor.getColumnIndex("location_name"));
		ticket_price = cursor.getString(cursor.getColumnIndex("ticket_price"));
		money_raised = cursor.getString(cursor.getColumnIndex("money_raised"));
		title = cursor.getString(cursor.getColumnIndex("title"));
		id = cursor.getInt(cursor.getColumnIndex("rag_id"));
		//cursor_id = cursor.getInt("cursor_id");
		sponsorship_min = cursor.getString(cursor.getColumnIndex("sponsorship_min"));
		//past_cursor_id = cursor.getInt("past_cursor_id");
		logo_id = cursor.getString(cursor.getColumnIndex("logo"));
		attendees = cursor.getInt(cursor.getColumnIndex("attendees"));
		capacity = cursor.getInt(cursor.getColumnIndex("capacity"));
		description = cursor.getString(cursor.getColumnIndex("description"));
		fb_eid = cursor.getString(cursor.getColumnIndex("fb_eid"));
		location_long = cursor.getLong(cursor.getColumnIndex("location_long"));
		require_tshirt_size = int2bool(cursor.getInt(cursor.getColumnIndex("tshirt")));
		open = int2bool(cursor.getInt(cursor.getColumnIndex("open")));
		require_passport = int2bool(cursor.getInt(cursor.getColumnIndex("passport")));
		require_emergency = int2bool(cursor.getInt(cursor.getColumnIndex("emergency")));
		require_doctor = int2bool(cursor.getInt(cursor.getColumnIndex("doctor")));
		require_hoody_size = int2bool(cursor.getInt(cursor.getColumnIndex("hoody")));
		require_address = int2bool(cursor.getInt(cursor.getColumnIndex("address")));
	
		signup_start = new Date(cursor.getLong(cursor.getColumnIndex("signup_start")));            
		date_start = new Date(cursor.getLong(cursor.getColumnIndex("date_start")));  
		signup_end = new Date(cursor.getLong(cursor.getColumnIndex("signup_end")));  
		sponsorship_deadline = new Date(cursor.getLong(cursor.getColumnIndex("sponsorship_deadline")));  
		date_end = new Date(cursor.getLong(cursor.getColumnIndex("date_end"))); 
		
	}
	
	public RagEvent(JSONObject event) throws MalformedURLException, ClassCastException, JSONException, IOException, URISyntaxException, InterruptedException, ParseException{
		this.event = event;
		
		this.getDetails();
	}
	
	public void getDetails() throws MalformedURLException, JSONException, IOException, URISyntaxException, ClassCastException, InterruptedException, ParseException{
		
		tagline = event.getString("tagline");
		
		location_lat = event.getLong("location_lat");
		location_name = event.getString("location_name");
		ticket_price = event.getString("ticket_price");
		money_raised = event.getString("money_raised");
		title = event.getString("title");
		id = event.getInt("id");
		//event_id = event.getInt("event_id");
		sponsorship_min = event.getString("sponsorship_min");
		//past_event_id = event.getInt("past_event_id");
		logo_id = event.getString("logo_id");
		
		try {
			attendees = event.getInt("attendees");
		} catch (Exception e) {
			attendees = 0;
		}
		
		capacity = event.getInt("capacity");
		description = event.getString("description");
		fb_eid = event.getString("fb_eid");
		location_long = event.getLong("location_long");
		
		require_tshirt_size = int2bool(event.getInt("require_tshirt_size"));
		open = int2bool(event.getInt("open"));
		require_passport = int2bool(event.getInt("require_passport"));
		require_emergency = int2bool(event.getInt("require_emergency"));
		require_doctor = int2bool(event.getInt("require_doctor"));	
		require_hoody_size = int2bool(event.getInt("require_hoody_size"));
		require_address = int2bool(event.getInt("require_address"));
		
		SimpleDateFormat mysql_date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		signup_start = mysql_date.parse(event.getString("signup_start"));            
		date_start = mysql_date.parse(event.getString("date_start"));  
		signup_end = mysql_date.parse(event.getString("signup_end"));  
		sponsorship_deadline = mysql_date.parse(event.getString("sponsorship_deadline"));  
		date_end = mysql_date.parse(event.getString("date_end"));  
	}

	private Boolean int2bool(int i){
		if(i == 1){
			return true;
		}else{
			return false;
		}
	}
	
	
	public void save(EventsData data) {
		//Insert a new record into the Events data source.
		SQLiteDatabase db = data.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("title", this.title);
		values.put("tagline", this.tagline);		
		values.put("logo", this.logo_id);
		values.put("description", this.description);
		values.put("date_start", this.date_start.getTime());
		values.put("date_end", this.date_end.getTime());
		values.put("location_name", this.location_name);
		values.put("location_long", this.location_long);
		values.put("location_lat", this.location_lat);
		values.put("ticket_price", this.ticket_price);
		values.put("sponsorship_min", this.sponsorship_min);
		values.put("sponsorship_deadline", this.sponsorship_deadline.getTime());
		values.put("signup_start", this.signup_start.getTime());
		values.put("signup_end", this.signup_end.getTime());
		values.put("capacity", this.capacity);
		values.put("money_raised", this.money_raised);
		values.put("attending", 0);//are you attending?? 1 yes, 0 no
		values.put("passport", this.require_passport);
		values.put("emergency", this.require_emergency);
		values.put("doctor", this.require_doctor);
		values.put("address", this.require_address);
		values.put("hoody", this.require_hoody_size);
		values.put("tshirt", this.require_tshirt_size);
		values.put("attendees", this.attendees);
		values.put("rag_id", this.id);
		values.put("open", this.open);
		values.put("fb_eid", this.fb_eid);
		db.insertOrThrow("events", null, values);
		db.close();
	}
	
	public Boolean isNew(){
		return true;
//		Log.i(Rag.TAG,"checking if new");
//		
//		SQLiteDatabase db = this.data.getReadableDatabase();
//		
//		String oldTitle = "";
//		
//		Cursor cursor = null;
//		try {
//			cursor = db.query("apod", new String[] {_ID, "title, date"}, 
//			        null, null, null, null, "date DESC");	
//			cursor.moveToFirst(); 
//			oldTitle = cursor.getString(cursor.getColumnIndex("title"));
//		} catch (Exception e) {
//			return true;
//		} finally {
//			db.close();
//			cursor.close();
//		}
//		
//		if(this.title.equals(oldTitle)){
//			return false;
//		} else {
//			return true;
//		}
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			Log.w(Rag.TAG, "1");
			return true;
		}
		if (obj == null){
			Log.w(Rag.TAG, "2");
			return false;
		}
		if (getClass() != obj.getClass()){
			Log.w(Rag.TAG, "3");
			return false;
		}
		RagEvent other = (RagEvent) obj;
		if (id != other.id){
			Log.w(Rag.TAG, "4");
			return false;
		}
		if (title == null) {
			if (other.title != null){
				Log.w(Rag.TAG, "5");
				return false;
			}
		} else if (!title.equals(other.title)){
			Log.w(Rag.TAG, "6");
			return false;
		}
		return true;
	}

	public boolean strictEquals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RagEvent other = (RagEvent) obj;
		if (attendees != other.attendees)
			return false;
		if (capacity != other.capacity)
			return false;
		if (date_end == null) {
			if (other.date_end != null)
				return false;
		} else if (!date_end.equals(other.date_end))
			return false;
		if (date_start == null) {
			if (other.date_start != null)
				return false;
		} else if (!date_start.equals(other.date_start))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (fb_eid == null) {
			if (other.fb_eid != null)
				return false;
		} else if (!fb_eid.equals(other.fb_eid))
			return false;
		if (id != other.id)
			return false;
		if (location_lat != other.location_lat)
			return false;
		if (location_long != other.location_long)
			return false;
		if (location_name == null) {
			if (other.location_name != null)
				return false;
		} else if (!location_name.equals(other.location_name))
			return false;
		if (logo_id == null) {
			if (other.logo_id != null)
				return false;
		} else if (!logo_id.equals(other.logo_id))
			return false;
		if (money_raised == null) {
			if (other.money_raised != null)
				return false;
		} else if (!money_raised.equals(other.money_raised))
			return false;
		if (open == null) {
			if (other.open != null)
				return false;
		} else if (!open.equals(other.open))
			return false;
		if (require_address == null) {
			if (other.require_address != null)
				return false;
		} else if (!require_address.equals(other.require_address))
			return false;
		if (require_doctor == null) {
			if (other.require_doctor != null)
				return false;
		} else if (!require_doctor.equals(other.require_doctor))
			return false;
		if (require_emergency == null) {
			if (other.require_emergency != null)
				return false;
		} else if (!require_emergency.equals(other.require_emergency))
			return false;
		if (require_hoody_size == null) {
			if (other.require_hoody_size != null)
				return false;
		} else if (!require_hoody_size.equals(other.require_hoody_size))
			return false;
		if (require_passport == null) {
			if (other.require_passport != null)
				return false;
		} else if (!require_passport.equals(other.require_passport))
			return false;
		if (require_tshirt_size == null) {
			if (other.require_tshirt_size != null)
				return false;
		} else if (!require_tshirt_size.equals(other.require_tshirt_size))
			return false;
		if (signup_end == null) {
			if (other.signup_end != null)
				return false;
		} else if (!signup_end.equals(other.signup_end))
			return false;
		if (signup_start == null) {
			if (other.signup_start != null)
				return false;
		} else if (!signup_start.equals(other.signup_start))
			return false;
		if (sponsorship_deadline == null) {
			if (other.sponsorship_deadline != null)
				return false;
		} else if (!sponsorship_deadline.equals(other.sponsorship_deadline))
			return false;
		if (sponsorship_min == null) {
			if (other.sponsorship_min != null)
				return false;
		} else if (!sponsorship_min.equals(other.sponsorship_min))
			return false;
		if (tagline == null) {
			if (other.tagline != null)
				return false;
		} else if (!tagline.equals(other.tagline))
			return false;
		if (ticket_price == null) {
			if (other.ticket_price != null)
				return false;
		} else if (!ticket_price.equals(other.ticket_price))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	public boolean inFuture() {
		Date currDate = new Date();
        return (currDate.compareTo(this.date_start) <= 0);
	}

	public void update(EventsData data) {
	    SQLiteDatabase db = data.getReadableDatabase();

		ContentValues values = new ContentValues();
		values.put("title", this.title);
		values.put("tagline", this.tagline);		
		values.put("logo", this.logo_id);
		values.put("description", this.description);
		values.put("date_start", this.date_start.getTime());
		values.put("date_end", this.date_end.getTime());
		values.put("location_name", this.location_name);
		values.put("location_long", this.location_long);
		values.put("location_lat", this.location_lat);
		values.put("ticket_price", this.ticket_price);
		values.put("sponsorship_min", this.sponsorship_min);
		values.put("sponsorship_deadline", this.sponsorship_deadline.getTime());
		values.put("signup_start", this.signup_start.getTime());
		values.put("signup_end", this.signup_end.getTime());
		values.put("capacity", this.capacity);
		values.put("money_raised", this.money_raised);
		values.put("attending", 0);//are you attending?? 1 yes, 0 no
		values.put("passport", this.require_passport);
		values.put("emergency", this.require_emergency);
		values.put("doctor", this.require_doctor);
		values.put("address", this.require_address);
		values.put("hoody", this.require_hoody_size);
		values.put("tshirt", this.require_tshirt_size);
		values.put("attendees", this.attendees);
		values.put("rag_id", this.id);
		values.put("open", this.open);
		values.put("fb_eid", this.fb_eid);
		
		db.update("events", values, "title LIKE '"+title+"'", null);
		
		db.close();
	}

	@Override
	public String toString() {
		return "RagEvent [attendees=" + attendees + ", capacity=" + capacity
				+ ", date_end=" + date_end + ", date_start=" + date_start
				+ ", description=" + description
				+ ", event_id=" + event_id + ", fb_eid=" + fb_eid + ", id="
				+ id + ", location_lat=" + location_lat + ", location_long="
				+ location_long + ", location_name=" + location_name
				+ ", logo_id=" + logo_id + ", money_raised=" + money_raised
				+ ", open=" + open + ", past_event_id=" + past_event_id
				+ ", require_address=" + require_address + ", require_doctor="
				+ require_doctor + ", require_emergency=" + require_emergency
				+ ", require_hoody_size=" + require_hoody_size
				+ ", require_passport=" + require_passport
				+ ", require_tshirt_size=" + require_tshirt_size
				+ ", signup_end=" + signup_end + ", signup_start="
				+ signup_start + ", sponsorship_deadline="
				+ sponsorship_deadline + ", sponsorship_min=" + sponsorship_min
				+ ", tagline=" + tagline + ", ticket_price=" + ticket_price
				+ ", title=" + title + "]";
	}

	public void delete(EventsData data){
	    SQLiteDatabase db = data.getReadableDatabase();
        Log.d(Rag.TAG, "Removing "+this.title);
        db.delete("events", "title LIKE '"+this.title+"'", null);
        db.close();
	}
	
}



